import AcroMLEngine from '../acroML.Engine.mjs';
import acroMLCulture from '../acroML.Culture.mjs';
import {MenuButton,Menu,MenuItem,SubMenu} from 'v3-easyui';

let AcroMLVueSelector={
  components:{MenuButton,Menu,MenuItem,SubMenu},
  data(){
    let LCIDs=[];
    let isFoundEnglish=false;
    for(let LCID in AcroMLEngine.instance.dd.LCIDList){
      if (LCID==1033) isFoundEnglish=true;
      LCIDs.push(LCID);
    }
    if (!isFoundEnglish){
      LCIDs.unshift(1033);
    }
    let langs=LCIDs.map(function(LCID){
      let lang=acroMLCulture.findCultureByLCID(LCID);
      return lang;
    });
    let langText=AcroMLEngine.instance.LCID;
    let lang=acroMLCulture.findCultureByLCID(AcroMLEngine.instance.LCID);
    if (lang) langText=this.getLangText(lang);
    return {
      langText,
      langs,
      AcroMLEngine
    }
  },
  methods:{
    onLangItemClick(LCID){console.log('onLangItemClick',LCID);
      AcroMLEngine.instance.LCID=LCID;
      AcroMLEngine.onSwitchLanguage();
      this.langText=AcroMLEngine.instance.LCID;
      let lang=acroMLCulture.findCultureByLCID(AcroMLEngine.instance.LCID);
      if (lang) this.langText=this.getLangText(lang);
    },
    getLangText(lang){
      return lang.LCID+','+lang.LanguageName_Native+','+lang.RegionName_Native;
    }
  },
  template:`
    <div>
      <MenuButton :plain="true" menuAlign="right" :text="t('Language')+': '+langText">
        <Menu :noline="true" @itemClick="onLangItemClick($event)">
          <MenuItem v-for="lang in langs" :value="lang.LCID" :text="getLangText(lang)"></MenuItem>
        </Menu>
      </MenuButton>
    </div>
  `
}
export default AcroMLVueSelector;
let VueSelector=AcroMLVueSelector;
export {VueSelector};